package com.iwic.smartkidsdaycares.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.iwic.smartkidsdaycares.R;

public class JSONUtils {
	private static final String TAG = "JSON Utils";
	private static final String TAG_CONTACTS = "results";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_LATITUDE = "lat";
    private static final String TAG_LONGITUDE = "lng";
    private static final String TAG_ADDRESS = "vicinity";
    private static final String TAG_ICON = "icon";
	private String json_text;
	private String filepath;
	private ArrayList<HospitalItem> masjids;
	private long file_length;
	public JSONUtils(String file_path){
		this.filepath=file_path;
		
	}
	public boolean isExist(){
		try {
			File file=new File(filepath);
			boolean ada=file.exists();
			
			return ada;
		} catch (Exception ex){
			return false;
		}
	}
	public void setJSONText(String value){
		this.json_text=value;
	}
	public String getJSONString(){
		this.loadJSONFile();
		return json_text;
	}
	public void loadJSONFile(){
    	String dummy="";	
    		 		
            //reading   
            try{
            	File file=new File(filepath);
                InputStream ips=new FileInputStream(file); 
                InputStreamReader ipsr=new InputStreamReader(ips);
                BufferedReader br=new BufferedReader(ipsr);
                String line;
                while ((line=br.readLine())!=null){
                    System.out.println(line);
                    dummy+=line+"\n";
                }
                br.close();
                ips.close();
                file_length=dummy.length();
                json_text=dummy;
            }       
            catch (Exception e){
                System.out.println(e.toString());
                 
            }
    	
    }
	public long getFileLength(){
		return file_length;
	}
	public void saveJSONtoFile(String value){
		PrintStream out = null;
		try {
			//String filepath=dirtemp+File.separator+jsonfile;
            File f=new File(filepath);
            if (f.exists()){
            	f.delete();
            }
//            BufferedWriter writer = new BufferedWriter(new FileWriter(f));
//            writer.write(value);
//            writer.close();
            out = new PrintStream(new FileOutputStream(filepath));
            out.print(value);
        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
        	if (out != null) out.close();
        }
		
	}
	public void loadAndSaveJSON(){
		this.loadJSONFile();
		this.saveJSONtoFile(this.json_text);
	}
	
	public ArrayList<HospitalItem> getMasjidList(){
		  
		try {
			masjids=new ArrayList<HospitalItem>();
            JSONObject jsonObj = new JSONObject(json_text);
             
            // Getting JSON Array node
            JSONArray masjid = jsonObj.getJSONArray(TAG_CONTACTS);
            int pjg=masjid.length();
            Log.d(TAG,"jumlah json row : "+Integer.toString(pjg));
            // looping through All Contacts
            for (int i = 0; i < pjg; i++) {
                JSONObject c = masjid.getJSONObject(i);
                JSONObject geometry=c.getJSONObject("geometry"); 
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                String latitude = geometry.getJSONObject("location").getString(TAG_LATITUDE);
                String longitude = geometry.getJSONObject("location").getString(TAG_LONGITUDE);
                String address = c.getString(TAG_ADDRESS);
                HospitalItem item=new HospitalItem(id, name, address, Double.parseDouble(latitude), Double.parseDouble(longitude));
                masjids.add(item);
            }
             return masjids;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
	}
}
